package com.byd.dms.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.byd.dms.entity.Employee;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Random;

/**
 * https://developer.aliyun.com/article/665456
 * https://jishuin.proginn.com/p/763bfbd5831d fastjson jackson
 * https://www.1024sou.com/article/693028.html  SpringDoc-OpenApi与Fastjson冲突——FastJsonHttpMessageConverter对String的默认处理
 * 巧用fastjson自定义序列化类实现字段的转换
 *
 * https://996geek.com/articles/341
 */
public class ToSystemNameSerializer implements ObjectSerializer {
    public static final ToSystemNameSerializer instance = new ToSystemNameSerializer();
    
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features) throws IOException {
        SerializeWriter out = serializer.out;
        
        System.out.println("ToSystemNameSerializer fieldName = " + fieldName);
        System.out.println("object = " + object);
        //System.out.println("fieldType = " + fieldType);
        //if ("1".equals(object)) {
        //    System.out.println("初始值的处理");
        //    out.writeString("hello world ");
        //    return;
        //}
        
        if (object == null) {
            //System.out.println("null 值不能输出");
            //out.writeString("我是NuLL ");
            out.writeNull();
            return;
        }
        
        //原来的值code
        String strVal = object.toString();
        //System.out.println(strVal);
       
        
        //通过code查找相关名称，测试先写死值, 这里可以进行DB查询 应对临时需求
        String name = "std" + new Random().nextInt(100);
        
        // 返回一个Employee对象
        Employee employee = new Employee(new Random().nextInt(100), "阿勇");
        
        String text = JSON.toJSONString(employee);
        //System.out.println(text);
        //Object o = JSON.toJSON(employee);
        //System.out.println(o.getClass().getName());
        //System.out.println(o.toString());
        //System.out.println("=============================");
        
        out.write(text);
        
    }
}
